/*
 * Copyright (c) 2024 iSoftStone Education Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "smart_home.h"
#include <stdio.h>
#include <stdbool.h>
#include "iot_errno.h"
#include "iot_pwm.h"
#include "iot_gpio.h"
#include "su_03t.h"
#include "wifi_tcp.h"
#include "lcd.h"
#include "picture.h"
#include "adc_key.h"
#include "components.h"
#include "string.h"

void lcd_menu_selected_move_up(); // 菜单左和右的处理
void lcd_menu_selected_move_down();
void light_menu_entry(lcd_menu_t *menu);                        // 照明灯菜单按下确认按键
void fan_menu_entry(lcd_menu_t *menu);                          // 风扇菜单按下确认按键
void auto_menu_entry(lcd_menu_t *menu);                         // 自动模式菜单按下确认按键
void set_current_menus(lcd_menu_t **new_menus, int num_menus);  // 设置当前菜单
void set_menu_entry(lcd_menu_t *menu, int menu_number, int db); // 设置页面
void control_menu_entry(void);                                  // 控制页面
void shwo_menu_entry(void);                                     // 显示页面
void main_menu_entry(void);                                     // 主菜单页面
    
// 全局变量定义
static bool auto_state = true;
static bool alarm_state = false;
static bool network_state = false;

// 控制菜单初始化数据
lcd_menu_t control_menu = {
    .img = {.img = NULL, .height = 0, .width = 0},
    .is_selected = false,
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "控制菜单"},
    .base_x = 10,
    .base_y = 32,
    .enterFunc = control_menu_entry,
    .exitFunc = NULL};

// 显示菜单初始化数据
lcd_menu_t shwo_menu = {
    .img = {.img = NULL, .height = 0, .width = 0},
    .is_selected = false,
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "数据显示"},
    .base_x = 10,
    .base_y = 64,
    .enterFunc = shwo_menu_entry,
    .exitFunc = NULL};

// 照明灯菜单初始化数据
lcd_menu_t light_menu = {
    .img = {.img = img_light_off, .height = 64, .width = 64},
    .is_selected = false,
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "灯光关"},
    .base_x = 10,
    .base_y = 32,
    .enterFunc = light_menu_entry,
    .exitFunc = NULL};

// 风扇菜单初始化数据
lcd_menu_t fan_menu = {
    .img = {.img = img_fan_off, .height = 64, .width = 64},
    .is_selected = false,
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "风扇关"},
    .base_x = 10,
    .base_y = 96,
    .enterFunc = fan_menu_entry,
    .exitFunc = NULL};

// 自动模式菜单初始化数据
lcd_menu_t auto_menu = {
    .img = {.img = NULL, .height = 0, .width = 0},
    .is_selected = false,
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "自动开"},
    .base_x = 10 + 64,
    .base_y = 160,
    .enterFunc = auto_menu_entry,
    .exitFunc = NULL};

// 温度面板初始化数据
lcd_display_board_t temp_db = {
    .img = {.img = img_temp_normal, .height = 48, .width = 48},
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "25.5°C"},
    .base_x = 10,
    .base_y = 32};

// 湿度面板初始化数据
lcd_display_board_t humi_db = {
    .img = {.img = img_humi, .height = 48, .width = 48},
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "25.5°C"},
    .base_x = 10,
    .base_y = 88};

// 亮度面板初始化数据
lcd_display_board_t lum_db = {
    .img = {.img = img_lum, .height = 48, .width = 48},
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "1234Lx"},
    .base_x = 10,
    .base_y = 146};

// 气体面板初始化数据
lcd_display_board_t ppm_db = {
    .img = {.img = img_combustible, .height = 48, .width = 48},
    .text = {.fc = LCD_MAGENTA, .bc = LCD_WHITE, .font_size = 24, .name = "20 ppm"},
    .base_x = 160,
    .base_y = 32};

// 所有面板集合数组
lcd_display_board_t *lcd_dbs[] = {&temp_db, &humi_db, &lum_db, &ppm_db};
#define menus_all_off -1

// 所有控制菜单集合数组
lcd_menu_t *lcd_menus_control[] = {&light_menu, &fan_menu, &auto_menu};
#define lcd_menus_control_number 3

// 主菜单集合数组
lcd_menu_t *lcd_menus_main[] = {&control_menu, &shwo_menu};
#define lcd_menus_main_number 2

// 当前菜单集合数组
lcd_menu_t **lcd_menus = NULL;
int db_show;
int lcd_menu_number;
static int menu_select_index;

// 菜单左和右的处理,需要考虑菜单个数的边界
void lcd_menu_selected_move_up()
{
    if (menu_select_index > 0)
    {
        menu_select_index--;
    }
}

void lcd_menu_selected_move_down()
{
    if (menu_select_index < lcd_menu_number - 1)
    {
        menu_select_index++;
    }
}

// 照明灯菜单按下确认按键
void light_menu_entry(lcd_menu_t *menu)
{
    int light_state = get_light_state();
    __set_auto_state(false);
    lcd_set_auto_state(false);
    if (light_state)
    {
        light_set_state(false);
        lcd_set_light_state(false);
    }
    else
    {
        light_set_state(true);
        lcd_set_light_state(true);
    }
}

// 风扇菜单按下确认按键
void fan_menu_entry(lcd_menu_t *menu)
{
    int motor_state = get_motor_state();
    __set_auto_state(false);
    lcd_set_auto_state(false);
    if (motor_state)
    {
        motor_set_state(false);
        lcd_set_motor_state(false);
    }
    else
    {
        motor_set_state(true);
        lcd_set_motor_state(true);
    }
}

// 自动模式菜单按下确认按键
void auto_menu_entry(lcd_menu_t *menu)
{
    int auto_state = auto_is_connected();
    if (auto_state)
    {
        __set_auto_state(false);
        lcd_set_auto_state(false);
    }
    else
    {
        __set_auto_state(true);
        lcd_set_auto_state(true);
    }
}

// 设置当前菜单
void set_current_menus(lcd_menu_t **new_menus, int num_menus)
{
    lcd_menus = new_menus;
    // 更新当前选中菜单的索引
    menu_select_index = 0;
    // 更新菜单数量
    lcd_menu_number = num_menus;
}

// 设置页面
void set_menu_entry(lcd_menu_t *menu, int menu_number, int db)
{
    lcd_fill(0, 0, LCD_W, LCD_H, LCD_WHITE); // 清屏
    if (db)
    {
        set_current_menus(NULL, -1);
    }
    else
    {
        set_current_menus(menu, menu_number);
    }
    db_show = db;
}

// 控制页面
void control_menu_entry(void)
{
    set_menu_entry(lcd_menus_control, lcd_menus_control_number, 0);
}

// 显示页面
void shwo_menu_entry(void)
{
    set_menu_entry(NULL, -1, 1);
}

// 主菜单页面
void main_menu_entry(void)
{
    set_menu_entry(lcd_menus_main, lcd_menus_main_number, 0);
}

// 初始化LCD
void lcd_dev_init(void)
{
    lcd_init(); // 初始化lcd
    main_menu_entry();
}

// 按键处理函数
void smart_home_key_process(int key_no)
{
    printf("smart_home_key_process:%d\n", key_no);
    if (key_no == KEY_UP)
    {
        lcd_menu_selected_move_up();
    }
    else if (key_no == KEY_DOWN)
    {
        lcd_menu_selected_move_down();
    }
    else if (key_no == KEY_LEFT)
    {
        main_menu_entry();
    }
    else if (key_no == KEY_RIGHT)
    {
        lcd_menu_entry(lcd_menus[menu_select_index]);
    }
}

// 物联网指令处理函数
void smart_home_iot_cmd_process(int iot_cmd)
{
    switch (iot_cmd)
    {
    case IOT_CMD_LIGHT_ON:
        light_set_state(true);
        lcd_set_light_state(true);
        break;
    case IOT_CMD_LIGHT_OFF:
        light_set_state(false);
        lcd_set_light_state(false);
        break;
    case IOT_CMD_MOTOR_ON:
        motor_set_state(true);
        lcd_set_motor_state(true);
        break;
    case IOT_CMD_MOTOR_OFF:
        motor_set_state(false);
        lcd_set_motor_state(false);
        break;
    case IOT_CMD_AUTO_ON:
        __set_auto_state(true);
        lcd_set_auto_state(true);
        break;
    case IOT_CMD_AUTO_OFF:
        __set_auto_state(false);
        lcd_set_auto_state(false);
        break;
    }
}

// 语音管家指令处理函数
void smart_home_su03t_cmd_process(int su03t_cmd)
{
    switch (su03t_cmd)
    {
    case auto_state_on:
        __set_auto_state(true);
        lcd_set_auto_state(true);
        break;
    case auto_state_off:
        __set_auto_state(false);
        lcd_set_auto_state(false);
        break;
    case light_state_on:
        light_set_state(true);
        lcd_set_light_state(true);
        break;
    case light_state_off:
        light_set_state(false);
        lcd_set_light_state(false);
        break;
    case motor_state_on:
        motor_set_state(true);
        lcd_set_motor_state(true);
        break;
    case motor_state_off:
        motor_set_state(false);
        lcd_set_motor_state(false);
        break;
    case temperature_get:
    {
        double temp, humi;
        sht30_read_data(&temp, &humi);
        su03t_send_double_msg(1, temp);
    }
    break;
    case humidity_get:
    {
        double temp, humi;
        sht30_read_data(&temp, &humi);
        su03t_send_double_msg(2, humi);
    }
    break;
    case illumination_get:
    {
        double lum;
        bh1750_read_data(&lum);
        su03t_send_double_msg(3, lum);
    }
    break;
    default:
        break;
    }
}

// PPM报警处理
void smart_home_ppm_alarm_process(tcp_alarm_t *alarm)
{
    // tcp_send_alarm(&alarm);
    if (alarm->ppm_alarm_state == true)
    {
        alarm->alarm_state = true;
    }
    else
    {
        alarm->alarm_state = false;
    }
    lcd_set_alarm_state(alarm->alarm_state);
}

// 光照报警处理
void smart_home_illumination_alarm_process(tcp_alarm_t *alarm)
{
    printf("illumination_alarm_process\n");
    if (alarm->humidity_alarm_state == true)
    {
        alarm->alarm_state = true;
    }
    else
    {
        alarm->alarm_state = false;
    }
    motor_set_state(alarm->alarm_state);
    lcd_set_motor_state(alarm->alarm_state);
    // lcd_menu_entry(&fan_menu);
}

// 加载LCD UI
void lcd_show_ui(void)
{
    lcd_show_chinese(96, 0, "环境监测", LCD_RED, LCD_WHITE, 32, 0);
    lcd_show_picture(280, 0, 32, 32, network_state ? img_wifi_on : img_wifi_off);
    lcd_show_picture(240, 0, 32, 32, alarm_state ? img_alarm_on : img_alarm_off);
    if (db_show == 0)
    {
        lcd_menu_update(lcd_menus, lcd_menu_number, menu_select_index);
        lcd_menu_show(lcd_menus, lcd_menu_number);
    }
    else
    {
        lcd_db_show(lcd_dbs, sizeof(lcd_dbs) / sizeof(lcd_display_board_t *));
    }
}

// 设置温度显示
void lcd_set_temperature(double temperature)
{
    sprintf(temp_db.text.name, "%.01f℃ ", temperature);
    /* 对温度做高温和正常的区分*/
    if (temperature > 35)
    {
        temp_db.text.fc = LCD_RED;
        temp_db.img.img = img_temp_high;
    }
    else
    {
        temp_db.text.fc = LCD_MAGENTA;
        temp_db.img.img = img_temp_normal;
    }
}

// 设置湿度显示
void lcd_set_humidity(double humidity)
{
    sprintf(humi_db.text.name, "%.01f%% ", humidity);
}

// 设置光照强度显示
void lcd_set_illumination(double illumination)
{
    sprintf(lum_db.text.name, "%.01fLx ", illumination);
}

// 设置PPM显示
void lcd_set_ppm(double ppm)
{
    sprintf(ppm_db.text.name, "%.01f ppm", ppm);
}

// 设置网络状态
void lcd_set_network_state(int state)
{
    network_state = state;
}

// 设置灯状态显示
void lcd_set_light_state(bool state)
{
    strcpy(light_menu.text.name, state ? "灯光开" : "灯光关");
    light_menu.img.img = state ? img_light_on : img_light_off;
}

// 设置电机状态显示
void lcd_set_motor_state(bool state)
{
    strcpy(fan_menu.text.name, state ? "风扇开" : "风扇关");
    fan_menu.img.img = state ? img_fan_on : img_fan_off;
}

// 设置自动模式状态显示
void lcd_set_auto_state(bool state)
{
    strcpy(auto_menu.text.name, state ? "自动开" : "自动关");
}

// 设置报警状态
void lcd_set_alarm_state(int state)
{
    alarm_state = state;
}

// 获取自动模式状态
unsigned int auto_is_connected(void)
{
    return auto_state;
}

// 设置自动模式状态（内部使用）
void __set_auto_state(int state)
{
    auto_state = state;
}
